package com.rede.didiok.admin.restapi.admin;


import java.util.Arrays;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.rede.didiok.admin.annotion.AuthorityVerify.AuthorityVerify;
import com.rede.didiok.admin.annotion.OperationLogger.OperationLogger;
import com.rede.didiok.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.rede.didiok.common.dto.rank.RankCategoryDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.feign.RankFeignClient;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;

/**
 * 榜单分类表 RestApi
 *
 * @author Rede
 * @date 2023年9月24日15:45:18
 */
@RestController
@RequestMapping("/admin/rankCategory")
@Api(value = "榜单分类相关接口", tags = {"榜单分类相关接口"})
@Slf4j
public class RankCategoryRestApi {

    @Autowired
    private RankFeignClient rankFeignClient;

    @AuthorityVerify
    @ApiOperation(value = "获取榜单分类列表", notes = "获取榜单分类列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody RankCategoryDto rankCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取榜单分类列表");
        return rankFeignClient.getRankCategoryPageList(rankCategoryDto);
    }

    @AvoidRepeatableCommit
    @AuthorityVerify
    @OperationLogger(value = "增加榜单分类")
    @ApiOperation(value = "增加榜单分类", notes = "增加榜单分类", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody RankCategoryDto rankCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加榜单分类");
        return rankFeignClient.addRankCategory(rankCategoryDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "编辑榜单分类")
    @ApiOperation(value = "编辑榜单分类", notes = "编辑榜单分类", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody RankCategoryDto rankCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑榜单分类");
        return rankFeignClient.editRankCategory(rankCategoryDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "批量删除榜单分类")
    @ApiOperation(value = "批量删除榜单分类", notes = "批量删除榜单分类", response = String.class)
    @PostMapping("/deleteBatch")
    public String delete(@Validated({Delete.class}) @RequestBody List<RankCategoryDto> rankCategoryDtoList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("批量删除榜单分类");
        return rankFeignClient.deleteBatchRankCategory(rankCategoryDtoList);
    }

    @AuthorityVerify
    @ApiOperation(value = "置顶分类", notes = "置顶分类", response = String.class)
    @PostMapping("/stick")
    public String stick(@Validated({Delete.class}) @RequestBody RankCategoryDto rankCategoryDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("置顶分类");
        return rankFeignClient.stickRankCategory(rankCategoryDto);

    }

    /**************************** RankCategoryMenu ***********************/

    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/menu/list")
    public String list(){
        return rankFeignClient.listWithTree();
    }


    /**
     * 根据分类id查询分类信息
     */
    @GetMapping("/detail")
    public String info(@RequestParam(value = "catId") String catId){
        return rankFeignClient.getCategoryById(catId);
    }

    /**
     * 保存
     */
    @PostMapping("/menu/save")
    public String save(@RequestBody RankCategoryDto category){
        return rankFeignClient.save(category);

    }

    /**
     * 修改
     */
    @PostMapping("/menu/update")
    public String update(@RequestBody RankCategoryDto category){
        return rankFeignClient.updateCascade(category);
    }

    /**
     * 修改分类排序
     */
    @PostMapping("/menu/update/sort")
    public String updateSort(@RequestBody RankCategoryDto[] category){

        return rankFeignClient.updateBatchById(Arrays.asList(category));
    }

    /**
     * 删除
     * @RequestBody获取请求体，必须发送POST请求
     * SpringMVC自动将请求体的数据(json),转为对应的对象
     */
    @PostMapping("/menu/delete")
    public String delete(@RequestBody Long[] catIds){

        return rankFeignClient.removeMenuByIds(Arrays.asList(catIds));

    }


}

